<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 管理后台首页
//-------------------------

namespace app\admin\controller;

use app\admin\Controller;
use think\Config;
use think\Exception;
use think\exception\HttpException;
use think\Loader;
use think\Session;
use think\Db;

class User extends Controller
{
    protected $fieldStatus = 'member_status';

    public function index()
    {
        $model = $this->getModel();

        // 列表过滤器，生成查询Map对象
        $map = $this->search($model, [$this->fieldIsDelete => $this::$isdelete]);

        // 特殊过滤器，后缀是方法名的
        $actionFilter = 'filter' . $this->request->action();
        if (method_exists($this, $actionFilter)) {
            $this->$actionFilter($map);
        }

        // 自定义过滤器
        if (method_exists($this, 'filter')) {
            $this->filter($map);
        }
        $this->datalist($model, $map);

        return $this->view->fetch();
    }

    protected function filter(&$map)
    {

        if ($this->request->param('member_mobile')) {
            $map['member_mobile'] = ["like", "%" . $this->request->param('member_mobile') . "%"];
        }
    }

    /**
     * 默认禁用操作
     */

    public function forbid()
    {
        return $this->updateField($this->fieldStatus, 0, "禁用成功");
    }

    /**
     * 默认恢复操作
     */
    public function resume()
    {
        return $this->updateField($this->fieldStatus, 1, "恢复成功");
    }

    /**
     * 编辑
     * @return string|\think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $controller = $this->request->controller();

        if ($this->request->isAjax()) {
            // 更新
            $data = $this->request->post();
            if(!preg_match("/^1[34578]\d{9}$/", $data['member_mobile'])){
                return ajax_return_adv_error("手机号不正确");
            }
            if (!$data['id']) {
                return ajax_return_adv_error("缺少参数ID");
            }
            $res = DB::table('tp_user')->where('id', $data['id'])->update($data);
            if ($res) {
                return ajax_return_adv('编辑成功','parent','编辑成功');
            }else{
                return ajax_return_adv_error("您未更新数据");
            }
        } else {
            // 编辑
            $id = $this->request->param('id');

            if (!$id) {
                throw new HttpException(404, "缺少参数ID");
            }
            $vo = $this->getModel($controller)->find($id);
            if (!$vo) {
                throw new HttpException(404, '该记录不存在');
            }

            $this->view->assign("vo", $vo);

            return $this->view->fetch();
        }
    }

    /**
     * 查看下级用户
     * @return string|\think\Response|\think\response\Json|\think\response\Jsonp|\think\response\Redirect|\think\response\View|\think\response\Xml
     * @throws Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function user()
    {
        $model = $this->getModel();

        $res['member_sub'] = $this->request->param('id');

        $this->datalist($model, $res);

        return $this->view->fetch();
    }

    /**
     * 二维码注册
     * @return string
     * @throws Exception
     */
    public function qr_code(){
        $param = request()->param();
        var_dump($param);
        return $this->view->fetch('user/qr_code');
    }
}